package com.game.hall;

import com.game.net.server.jpbsocket.JPbSocketNettyServerHandler;
import com.game.message.Msg;
import com.game.message.connetion.ConnectionManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by legend9999 on 2016/4/25.
 */
@Component
@ChannelHandler.Sharable
public class HallJPBHandler extends JPbSocketNettyServerHandler {
    private static Logger logger = LoggerFactory.getLogger(HallBiz.class);
    @Autowired
    private HallBiz hallBiz;

    @Override
    public void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
        logger.info(msg.toString());
        hallBiz.onMsg(msg, ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ctx.attr(ConnectionManager.userIdAttributeKey).set(0);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
        ConnectionManager.getInstance().remove(ctx);
    }
}

